﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data;
using System.Drawing;

namespace HospitalSystem
{
    public partial class RecordChildData : System.Web.UI.Page
    {
        DataClasses1DataContext db = new DataClasses1DataContext();
        //int analysistime;
        //int comparetoBreakLoop;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Panel2.Visible = true;
                Panel1.Visible = false;
                string ID = (String)Session["ID"];
                string caseNumber = (String)Session["CaseNumber"];

                Label3.Text = ID;
                Label7.Text = caseNumber;
                var queryShowProfile = from a in db.Persons
                                       where a.CID.Equals(ID)
                                       select a;
                //Show Profile @ Label
                foreach (var qP in queryShowProfile)
                {
                    Label4.Text = qP.FirstName;
                    Label5.Text = qP.LastName;

                    //int age = DateTime.Now.Year - qP.DateofBirth.Year;
                    //if (DateTime.Now < qP.DateofBirth.AddYears(age))
                    //{
                    //    age--;
                    //}

                    //Label6.Text = age.ToString();
                }

                //Get Value from URL after "?" 
                string choosingValue = Request.QueryString["ids"];
                var query = from a in db.Activities
                            where a.ACTCode.Equals(choosingValue)
                            select a.ACTDesc;
                if (query.SingleOrDefault() == null)
                {
                    //Panel2.Visible = false;
                    Panel1.Visible = false;
                    Label1.Text = "เลือกทักษะพัฒนาการเพื่อบันทึกข้อมูลพัฒนาการ";
                }
                else
                {
                    Panel1.Visible = true;
                    Button3.Visible = false;
                    foreach (var q in query)
                    {
                        Label1.Text = q;
                    }

                }
            }
        }
        private List<SubActivity> GetMainActValue(string type)
        {
            return (from a in db.SubActivities
                    where a.ACTCode.Equals(type)
                    select a).ToList();
        }

        private void boundDataDropdown(string typeID)
        {
            RadioButtonList1.DataSource = GetMainActValue(typeID);
            RadioButtonList1.DataTextField = "SACTDesc";
            RadioButtonList1.DataValueField = "ACTCode";
            RadioButtonList1.DataBind();
        }

        
        protected void RadioButtonList1_SelectedIndexChanged1(object sender, EventArgs e)
        {
            int i = 0;
            string[] arrList = new string[1000];
            Button3.Visible = true;
            DataTable dt = new DataTable();
            dt.Columns.Add("AgeRange");
            dt.Columns.Add("SubActivityCode");
            dt.Columns.Add("Description");
            dt.Columns.Add("SACTValue");
            //Show Value @ Data Gridview
            var query = from a in db.SubActivity2s
                        where a.SACTCode.Equals(RadioButtonList1.SelectedValue)
                        orderby a.SACT2Code
                        select a.SACT2Code;
            foreach (string q in query)
            {
                var query2 = from a in db.GrowthEvaluation2s
                             where a.CaseNo.Equals(Label7.Text) && a.SACT2Code.Equals(q) && a.CID.Equals(Label3.Text)
                             select a;
                //Nodata in ChildService DB
                if (query2.FirstOrDefault() == null)
                {
                    var query3 = from a in db.SubActivity2s
                                 join b in db.AgeTypes on a.AgeCode equals b.AgeCode
                                 where a.SACT2Code.Equals(q)
                                 select new
                                 {
                                     b.AgeRange,
                                     a.SACT2Code,
                                     a.SACT2Desc
                                 };

                    foreach (var q3 in query3)
                    {
                        arrList[i] = q3.AgeRange.ToString();
                        arrList[i + 1] = q3.SACT2Code.ToString();
                        arrList[i + 2] = q3.SACT2Desc.ToString();
                        arrList[i + 3] = "0";
                        i = i + 4;
                    }

                }

                //Having Data in ChildService DB
                else
                {
                    var query4 = from r1 in
                                     (from a in db.GrowthEvaluation2s
                                      join b in db.SubActivity2s on a.SACT2Code equals b.SACT2Code
                                      where b.SACTCode.Equals(RadioButtonList1.SelectedValue) && b.SACT2Code.Equals(q)
                                      select new
                                      {
                                          a.SACTValue,
                                          b.SACT2Code,
                                          b.AgeCode,
                                          b.SACT2Desc,
                                          a.CaseNo,
                                          a.CID
                                      })
                                 join c in db.AgeTypes on r1.AgeCode equals c.AgeCode
                                 where r1.CaseNo.Equals(Label7.Text) && r1.CID.Equals(Label3.Text)
                                 orderby r1.SACT2Code
                                 select new
                                 {
                                     c.AgeRange,
                                     r1.SACT2Code,
                                     r1.SACT2Desc,
                                     r1.SACTValue
                                 };
                    foreach (var q4 in query4)
                    {
                        arrList[i] = q4.AgeRange.ToString();
                        arrList[i + 1] = q4.SACT2Code.ToString();
                        arrList[i + 2] = q4.SACT2Desc.ToString();
                        arrList[i + 3] = q4.SACTValue;
                        i = i + 4;
                    }
                }
            }
            i = 0;
            int count = 0;
            var query5 = from a in db.SubActivity2s
                        where a.SACTCode.Equals(RadioButtonList1.SelectedValue)
                        select a;
            foreach (var q5 in query5)
            {
                count++;
            }
            for (int j = 0; j < count; j++)
            {

                dt.Rows.Add();
                dt.Rows[i]["AgeRange"] = arrList[(4 * j)];
                dt.Rows[i]["SubActivityCode"] = arrList[(4 * j) + 1];
                dt.Rows[i]["Description"] = arrList[(4 * j) + 2];
                dt.Rows[i]["SACTValue"] = arrList[(4 * j) + 3];
                i++;

            }
            GridView1.DataSource = dt;
            GridView1.DataBind();
        }

        protected void Button2_Click1(object sender, EventArgs e)
        {
            string choosingValue = Request.QueryString["ids"];
            boundDataDropdown(choosingValue);
        }

        private void MessageBox(string msg)
        {
            Label lbl = new Label();
            lbl.Text = "<script language='javascript'>" + Environment.NewLine + "window.alert('" + msg + "')</script>";
            Page.Controls.Add(lbl);
        }

        protected void Button3_Click(object sender, EventArgs e)
        {
            try
            {
                
                foreach (GridViewRow GR in GridView1.Rows)
                {
                    GrowthEvaluation2 GE = new GrowthEvaluation2();
                    RadioButtonList rb = GR.FindControl("rbl") as RadioButtonList;
                    var query = from a in db.GrowthEvaluations
                                where a.CaseNo.Equals(Label7.Text) && a.SACTCode.Equals(GR.Cells[1].Text)
                                select a;
                    //Insert into if no Value in GrowthEvaluations DB
                    if (query.SingleOrDefault() == null)
                    {
                        GE.CID = Label3.Text;
                        GE.CaseNo = int.Parse(Label7.Text);
                        GE.SACT2Code = GR.Cells[1].Text;
                        GE.SACTValue = rb.SelectedValue;
                        db.GrowthEvaluation2s.InsertOnSubmit(GE);
                        db.SubmitChanges();
                    }
                    //Update if Having Value in GrownthEvaluations DB
                    else
                    {
                        foreach (var q in query)
                        {
                            q.SACTValue = rb.SelectedValue;

                        }
                        db.SubmitChanges();
                    }
                }
                MessageBox("บันทึกหรือดัดแปลงข้อมูลสำเร็จ");
            }
            catch (Exception)
            {
                MessageBox("Error !!!!");
            }

            
        }

        

        
    }
}